//
//  ZZRouter.m
//  zhengzai-tv
//
//  Created by 孙超 on 16/3/20.
//  Copyright © 2016年 zhengzai.tv. All rights reserved.
//

#import "ZZRouter.h"
#import "ZZNavigationControllerStack.h"

#ifndef kAppDelegate
#define kAppDelegate [UIApplication sharedApplication].delegate
#endif

@interface ZZRouter ()

@property (nonatomic, copy) NSMutableDictionary *viewModelViewMappings;

@end

@implementation ZZRouter

DEF_SINGLETON

- (NSMutableDictionary *)viewModelViewMappings
{
    if (_viewModelViewMappings == nil) {
        _viewModelViewMappings = [NSMutableDictionary dictionary];
    }
    return _viewModelViewMappings;
}

- (ZZBaseViewController *)viewControllerForViewModel:(ZZBaseViewModel *)viewModel
{
    NSString *viewController = self.viewModelViewMappings[NSStringFromClass(viewModel.class)];
    
//    NSParameterAssert([NSClassFromString(viewController) isSubclassOfClass:[ZZBaseViewController class]]);
    NSParameterAssert([NSClassFromString(viewController) instancesRespondToSelector:@selector(initWithViewModel:)]);
    
    return [[NSClassFromString(viewController) alloc] initWithViewModel:viewModel];
}

- (void)registViewController:(NSString *)controller withViewModel:(NSString *)viewModel
{
    [self.viewModelViewMappings setObject:controller forKey:viewModel];
}

- (void)pushViewModel:(ZZBaseViewModel *)viewModel animated:(BOOL)animated
{
    UIViewController *viewController = (UIViewController *)[self viewControllerForViewModel:viewModel];
    viewController.hidesBottomBarWhenPushed = YES;
    [[ZZNavigationControllerStack topNavigationController] pushViewController:viewController animated:animated];
}

- (void)popViewModelAnimated:(BOOL)animated
{
    [[ZZNavigationControllerStack topNavigationController] popViewControllerAnimated:animated];
}

- (void)popToRootViewModelAnimated:(BOOL)animated
{
    [[ZZNavigationControllerStack topNavigationController] popToRootViewControllerAnimated:animated];
}

- (void)presentViewModel:(ZZBaseViewModel *)viewModel animated:(BOOL)animated completion:(void(^)(void))completion
{
    UIViewController *viewController = (UIViewController *)[self viewControllerForViewModel:viewModel];
    
    UINavigationController *presentingViewController = [ZZNavigationControllerStack topNavigationController];
    if (![viewController isKindOfClass:UINavigationController.class]) {
        viewController = [[UINavigationController alloc] initWithRootViewController:viewController];
    }
    [ZZNavigationControllerStack pushNavigationController:(UINavigationController *)viewController];
    
    [presentingViewController presentViewController:viewController animated:animated completion:completion];
}

- (void)dismissViewModelAnimated:(BOOL)animated completion:(void(^)(void))completion
{
    [ZZNavigationControllerStack popNavigationController];
    [[ZZNavigationControllerStack topNavigationController] dismissViewControllerAnimated:animated completion:completion];
}

- (void)resetRootViewModel:(ZZBaseViewModel *)viewModel
{
    [ZZNavigationControllerStack clear];
    
    UIViewController *viewController = (UIViewController *)[self viewControllerForViewModel:viewModel];
    
    if ([viewController isKindOfClass:[UINavigationController class]]) {
        [ZZNavigationControllerStack pushNavigationController:(UINavigationController *)viewController];
    } else {
        viewController = [[UINavigationController alloc] initWithRootViewController:viewController];
        [ZZNavigationControllerStack pushNavigationController:(UINavigationController *)viewController];
    }
    
    kAppDelegate.window.rootViewController = viewController;
}

@end
